const userDao = require("../dao/userDao");

module.exports = {
    //查询
    async AllUsers() {
        return await userDao.AllUser();
    },
    //注册
    async register({
        name,
        pwd,
        photo
    }) {
        let result = await userDao.QueryName(name);
        if (result) {
            return ({
                code: '0',
                msg: '账号已注册'
            })
        } else {
            await userDao.RegisteredUsers({
                name,
                pwd,
                photo
            });
            return ({
                code: '1',
                msg: '账号可注册'
            })
        }
    },
    //登录
    async login(name, pwd,photo) {
        return await userDao.QueryUser(name, pwd,photo);
    },

    //删除
    async delete(usersId) {
        return await userDao.deleteUser({
            '_id': usersId
        });
    },
    //查询完整信息
    async completeInformations(userId) {
        return await userDao.completeInformation(userId)
    },
    //修改用户
    async modifyUsers({
        '_id': userId
    }, {
        name,
        pwd,
        photo
    }) {
        return await userDao.modifyUser({
            '_id': userId
        }, {
            name,
            pwd,
            photo
        })
    },
    //分页
    async getAccessToPag({
        currentpage,
        pagesize
    }) {
        //创建一个空对象
        let pagedata = {};
        //当前页数
        pagedata.currentpage = currentpage;
        //每页显示的条数
        pagedata.pagesize = pagesize;
        //显示的数据
        pagedata.data = await userDao.accessToPag({
            currentpage,
            pagesize
        });
        //总条数
        pagedata.totalInt = await userDao.getsNumber();
        //总页数
        pagedata.totalSize = Math.ceil(pagedata.totalInt / pagesize);
        return pagedata
    },
}